package com.me.videoservice.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.me.videoservice.dao.VideoM3u8Dao;
import com.me.videoservice.model.callback.HlsCallBack;
import com.me.videoservice.model.entry.VideoM3u8Info;
import com.me.videoservice.service.VideoM3u8Service;
import java.time.LocalDateTime;
import java.util.List;
import org.springframework.stereotype.Service;

@Service
public class VideoM3u8ServiceImpl extends ServiceImpl<VideoM3u8Dao, VideoM3u8Info> implements
    VideoM3u8Service {

  @Override
  public List<VideoM3u8Info> queryAll() {
    return this.query().orderByDesc("push_s_date").list();
  }

  @Override
  public void hls(HlsCallBack callback) {
    LocalDateTime now = LocalDateTime.now();
    Integer count = this.query()
        .eq("m3u8_url", callback.getM3u8_url())
        .count();
    if (count > 0) {
      this.update(Wrappers.<VideoM3u8Info>update()
          .set("push_u_date", now)
          .set("end_ts", callback.getUrl())
          .eq("m3u8_url", callback.getM3u8_url()));
    } else {
      this.save(new VideoM3u8Info(
          null,
          callback.getAction(),
          callback.getClientId(),
          null,
          callback.getIp(),
          callback.getVhost(),
          callback.getApp(),
          callback.getStream(),
          callback.getParam(),
          now,
          now,
          callback.getM3u8_url(),
          callback.getUrl(),
          null
      ));
    }
  }

}
